/**
 * Created by way on 2016/12/18.
 */
import Router from '../lib/krouter';
import cfg from '../config';
import { get, token } from '../util/api';
// import * as ajax from '../lib/kajax';
import { urlParam } from '../lib/kutil';
import { strfind, urlParams } from '../util/tool';

import Join from './join';
import Evaluate from './evaluate';
import List from './list';
import Deltail from './deltail';

let _pid = null;
let _mobile = null;
let _name = null;
let _token = null;//参数
let _userInfo = null;//参数
let code = null;//参数
let opts = {};//参数
let sid = null;
let tab = null;
let roster = null;
// test
// let users = {
// 	"name": "小欢同学",
// 	"mobile": "13883827248",
// 	"phone": "",
// 	"depart": "公司部",
// 	"branch": "- 请选择 -",
// 	"shop": "",
// 	"shopNo": "",
// 	"pid": "73848448",
// 	"servicePid": [],
// 	"role": [
// 		"专家"
// 	],
// 	"perm": [
// 		13
// 	],
// 	"skill": [],
// 	"subed": {
// 		"pic": [],
// 		"msg": [],
// 		"all": [],
// 		"sta": []
// 	},
// 	"sub": {
// 		"pic": [],
// 		"msg": [],
// 		"all": [],
// 		"sta": []
// 	},
// 	"member": [],
// 	"group": [],
// 	"contact": [],
// 	"friend": [],
// 	"tag": [],
// 	"openid": [
// 		null,
// 		[
// 			"11",
// 			"Rg7vpxeuW3L6IVsZ0/j0iEKm7qqDiJt5"
// 		]
// 	],
// 	"__v": 0,
// 	"tab": "1",
// 	"sid": "11",
// 	"headimgurl": "https://im.icbc.com.cn/ICBCMPServer/servlet/ICBCGetAvailableImgPathServlet?dir=userinfo/1053630781_poc.jpg"
// };
let users = {
    "name" : "网点",
    "mobile" : "17384791611",
    "phone" : "",
    "depart" : "- 请选择 -",
    "branch" : "朝天门",
    "shop" : "朝天门支行",
    "shopNo" : "270",
    "pid" : "42778303",
    "servicePid" : [],
    "role" : [ 
        "网点"
    ],
    "perm" : [ 
        13
    ],
    "skill" : [],
    "subed" : {
        "pic" : [],
        "msg" : [],
        "all" : [],
        "sta" : []
    },
    "sub" : {
        "pic" : [],
        "msg" : [],
        "all" : [],
        "sta" : []
    },
    "member" : [],
    "group" : [],
    "contact" : [],
    "friend" : [],
    "tag" : [],
    "openid" : [ 
        null, 
        [ 
            "11", 
            "Rg7vpxeuW3LiD1pahHokiGo8lUo+JJzW"
        ]
    ],
    "__v" : 0,
    "tab" : "1",
    "sid" : "11",
    "headimgurl" : "https://im.icbc.com.cn/ICBCMPServer/servlet/ICBCGetAvailableImgPathServlet?dir=userinfo/1093109434_poc.jpg"
}
const router = new Router({
	container: '#dvContainer'
});

document.ready(() => {
	// return;
	router
		.push(new Join())
		.push(new Evaluate())
		.push(new Deltail())
		.push(new List());

	code = urlParam('code');
	opts = getUrlParamFun();
	sid = urlParam('sid') || 11;
	tab = urlParam('tab') || 1;
	// alert(`entry2  ${JSON.stringify(opts)}`);
	// console.log(cfg.dev.token);
	if (cfg.dev.token)
		getUser();
	else {
		if (!code) {
			jumpUrl(users);
			// router.go('/ogs/list', '', true);
		} else {
			getUser();
		}
	}

});

// 获取用户身份信息
function getUser() {
	get(`${cfg.ogs.host}${cfg.ogs.userInfo}`, 'type=self', (rs) => {
		let ok = false;
		if (rs) {
			const r = JSON.parse(rs);
			console.log("getUser:", r);
			if (r.rc === 200) {
				ok = true;
				const user = r.relInfoRes;
				// _name = r.relInfoRes.name; // r.userInfo.name;
				// _mobile = r.relInfoRes.mobile; // r.userInfo.mobile;
				// _pid = r.relInfoRes.pid; // r.userInfo.pid;
				getSiteParam(opts, user);
			}
		}
	});
}

//根据perm 判断用户角色
function jumpUrl(data) {
	// console.log(data);
	// alert(`jumpUrl(用户信息):${JSON.stringify(data)}`);
	// let perm = [['10', "专职坐席"], ['11', "对公专家成员"], ['12', "高级别领导"], ["13", "最高级别领导"], ['14', "网点线下服务"], ['15', "支行高级别管理人员"], ['16', "总行领导"]];
	// let permArr = [10, 11, 12, 13, 14, 15, 16];
	// let skill = [[10, "金融理财"], [11, "行用卡"], [12, "贷款"]];
	if (!data) return;
	data = data instanceof Object ? data : JSON.parse(data);
	let urls = '/ogs/list';
	// console.log("首页传值参数", data);
	let perm = data.perm[0];
	const tempData = {
		userInfo: data,
		issueid: opts.issueId || '',
		form: data.pid,
		to: '',
		opts: opts || {}
	};
	// console.log(permArr);
	// console.log(data.perm);
	// console.log(strfind(permArr, data.perm));
	if (perm == 10 || perm == 11) {
		//客户
		urls = '/ogs/join';
		// alert(JSON.stringify(opts));
		if (opts.issueId && opts.pid) {
			//用户评价
			urls = '/ogs/evaluate';
		}
		tempData.opts.perm = perm;
	} else {
		//专家
		urls = '/ogs/list';
		tempData.opts.perm = perm;
	}
	router.go(urls, tempData, true);
}
// 获取url参数
function getUrlParamFun() {
	let rc = {};

	rc.sid = urlParam('sid') || urlParams('sid');
	rc.tab = urlParam('tab') || urlParams('tab');
	rc.issueId = urlParam('issueId') || urlParams('issueId');
	rc.form = urlParam('form') || urlParams('form');
	rc.to = urlParam('to') || urlParams('to');
	rc.pid = urlParam('pid') || urlParams('pid');
	rc.perm = urlParam('perm') || urlParams('perm');
	return rc;
}
// site
function getSiteParam(opt, user) {
	get(`${cfg.ogs.host}${cfg.ogs.siteParam}`, `sid=${opt.sid || '11'}&tab=${opt.tab || '1'}`, rs => {
		if (rs) {
			// console.log(rs);
			let r = JSON.parse(rs);
			if (r) {
				r = r.ErrMsg[0];
				localStorage.setItem(cfg.app.nauoyaSite, JSON.stringify(r));
				jumpUrl(user);
			}
		}
	});
}

